﻿Namespace SayWindow

    Module mSayWindow

        Public WithEvents SayWindow As GUI.MirWindow
        Public WithEvents HideSay As GUI.MirButton

        Private SayText As String

        Private bIsVisible As Boolean

        Private posX As Integer, posY As Integer

        Public Sub Init()

            SayWindow = New GUI.MirWindow("SayWindow", posX, posY, 387, 282, True)
            HideSay = New GUI.MirButton(SayWindow, "HideSay", 354, 29 + Libs.GameInter.Image(381).Header.Height + Libs.GameInter.Image(382).Header.Height, 0.0F, ImgLibs.GameInter, 0, 3036, 3036)

            bIsVisible = False

        End Sub

        Public Sub Shutdown()

            SayWindow.Destory()

        End Sub

        Public Sub Show()

            If Not bIsVisible Then
                Windows.AddWindow(SayWindow,Windows.WindowType.AlwaysOnTop)
                bIsVisible = True
            End If

        End Sub

        Public Sub Hide()

            If bIsVisible Then
                ShopWindow.Hide()
                Windows.RemoveWindow(SayWindow)
                bIsVisible = False
            End If

        End Sub

        Public Sub Say(struct As EvoLib.Protocol.SayInfo)

            If SayWindow Is Nothing Then Init()

            posX = SayWindow.mPosX
            posY = SayWindow.mPosY

            Shutdown()
            Init()

            SayText = struct.Text
            Show()

            Dim tmpNameIdx As Integer

            Dim c As Color = Color.White
            Dim isBold As Boolean
            Dim x As Integer, y As Integer

            Dim isTag As Boolean, isClosing As Boolean, Tag As String = ""
            Dim isParam As Boolean, ParamName As String = "", isParamStr As Boolean, ParamStr As String = "", aParams As New Collection
            Dim currentChr As String = ""
            Dim isLink As Boolean
            Dim strCMD As String = "", strLink As String = ""


            y = 15
            x = 18

            For i As Integer = 1 To SayText.Length

                currentChr = UCase(Mid(SayText, i, 1))

                If isTag Then

                    If currentChr = " " Then

                        If ParamName.Length > 0 Then
                            aParams.Add(ParamStr, UCase(ParamName))
                        End If

                        isParam = True
                        isParamStr = False
                        ParamName = ""
                        ParamStr = ""
                        aParams = New Collection

                    ElseIf currentChr = "/" Then
                        isClosing = True

                    ElseIf currentChr = ">" Then

                        isTag = False
                        isParam = False
                        isParamStr = False

                        If ParamName.Length > 0 Then
                            aParams.Add(ParamStr, UCase(ParamName))
                        End If

                        'todo: process tags
                        If UCase(Tag) = "B" Then

                            If isClosing Then
                                isBold = False
                            Else
                                isBold = True
                            End If

                        ElseIf UCase(Tag) = "C" Then

                            If isClosing Then
                                c = Color.White

                            Else

                                c = Color.FromName(aParams("NAME"))

                            End If

                        ElseIf UCase(Tag) = "A" Then

                            If isClosing Then
                                isLink = False

                                Dim arrC As New ArrayList
                                arrC.Add(c)

                                Dim tmp As New GUI.MirText(SayWindow, "SayCmd" & strCMD, x, y, DirectX.ProFont9, "\0" & strLink, Color.Transparent, Color.Transparent, arrC, , , , , True, , Color.White)
                                x += 6 * strLink.Length

                                AddHandler tmp.Click, AddressOf ALinkClick

                                SayWindow.AddInterfaceItem(tmp)
                            Else
                                isLink = True
                                strCMD = aParams("CMD")
                                strLink = ""
                            End If

                        ElseIf UCase(Tag) = "BR" Then

                            x = 18
                            y += 9

                        ElseIf UCase(Tag) = "SPACE" Then

                            Dim spaceCount As Integer = aParams("COUNT")

                            x += spaceCount * 6

                        End If

                    ElseIf isParam And Not isParamStr Then

                        If currentChr = "=" Then
                            isParamStr = True
                        Else
                            ParamName &= currentChr
                        End If

                    ElseIf isParam And isParamStr Then

                        If Not (currentChr = Chr(34)) Then
                            ParamStr &= Mid(SayText, i, 1)
                        End If

                    Else

                        Tag &= currentChr

                    End If


                ElseIf currentChr = "<" Then

                    isClosing = False
                    isTag = True
                    isParam = False
                    aParams = New Collection
                    ParamName = ""
                    ParamStr = ""
                    Tag = ""

                ElseIf currentChr = Chr(10) Or currentChr = Chr(13) Then
                    'CR or LF

                ElseIf isLink Then

                    strLink &= currentChr

                Else

                    Dim arrC As New ArrayList
                    arrC.Add(c)

                    Dim tmp As New GUI.MirText(SayWindow, "Say" & tmpNameIdx, x, y, DirectX.ProFont9, "\0" & Mid(SayText, i, 1), Color.Transparent, Color.Transparent, arrC)
                    x += 6

                    SayWindow.AddInterfaceItem(tmp)
                    tmpNameIdx += 1

                End If

            Next


        End Sub

        Public Sub ALinkClick(name As String)

            If Left(name, 6) = "SayCmd" Then

                Dim strCMD As String = Right(name, name.Length - 6)

                If UCase(strCMD) = "EXIT" Then

                    Hide()

                Else

                    ScriptCommand(strCMD)
                    Hide()

                End If

            End If

        End Sub

        Public Sub ScriptCommand(strCMD As String)

            Dim struct As NPCCall
            struct.CMD = strCMD

            Dim pkt As Packet
            pkt.PacketType = GamePacketType.NPCCall
            pkt.PacketOpCode = PacketOpCode.Enquiry
            pkt.Username = Globals.sUsername

            ClientSock.gSock.SendData(MakePacket(pkt, struct, Globals.Crypto))

        End Sub

        Private Sub SayWindow_DrawWindow(ByRef mode As Globals.DrawingMode, ByRef x As Integer, ByRef y As Integer, ByRef z As Single) Handles SayWindow.DrawWindow

            If mode = Globals.DrawingMode.Normal Then

                Libs.GameInter.Image(381).Draw(x, y, z)
                Libs.GameInter.Image(382).Draw(x + 3, y + Libs.GameInter.Image(381).Header.Height, z)
                Libs.GameInter.Image(384).Draw(x + 1, y + Libs.GameInter.Image(381).Header.Height + Libs.GameInter.Image(382).Header.Height, z)

            End If

        End Sub

        Private Sub HideSay_Click() Handles HideSay.Click
            Hide()

        End Sub
    End Module

End Namespace